package 中等.队列;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两
 * 个时间的最小时间差并以分钟数表示。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/minimum-time-difference/
 */
public class 最小时间差_539 {

    public static void main(String[] args) {

        // '1' -> 1
        char c9 = '1';
        int num9 = c9 - '0';
        System.out.println(num9);

        int num10 = 1;
        char c10 = (char) (num10 + '0');
        System.out.println(c10);

        String str = "01:03";
        System.out.println(getMinutes(str));

        //["05:31","22:08","00:35"]
        List<String> timePoints = new ArrayList<>();
        timePoints.add("00:35");
        timePoints.add("05:31");
        timePoints.add("22:08");
        System.out.println(efficientFindMinDifference(timePoints));

    }

    /**
     * 排序
     *
     * @param timePoints
     * @return
     */
    public static int findMinDifference(List<String> timePoints) {
        Integer[] timePointstoNum = new Integer[timePoints.size()];
        for (int i = 0; i < timePoints.size(); i++) {
            String[] timePoint = timePoints.get(i).split(":");
            timePointstoNum[i] = Integer.parseInt(timePoint[0]) * 60 + Integer.parseInt(timePoint[1]);
        }
        Arrays.sort(timePointstoNum);
        int preMinTime = timePointstoNum[1] - timePointstoNum[0];
        for (int i = 2; i < timePointstoNum.length; i++) {
            int curTime = timePointstoNum[i] - timePointstoNum[i - 1];
            preMinTime = Math.min(preMinTime, curTime);
        }
        //0->1->2->0  最后一步形成一个环，开始位置加一天->00:00:00->24:00:00
        preMinTime = Math.min(preMinTime, 24 * 60 + timePointstoNum[0] - timePointstoNum[timePointstoNum.length - 1]);
        return preMinTime;
    }

    /**
     * 抽屉原理
     * 一天最多表示24*60种分钟，如果给出的时间列表数量大于24*60，那么
     * 一定存在两个相等的时间
     *
     * @param timePoints
     * @return
     */
    public static int efficientFindMinDifference(List<String> timePoints) {
        int length = timePoints.size();
        if (length > 24 * 60) {
            return 0;
        }
        Integer[] timePointstoNum = new Integer[timePoints.size()];
        for (int i = 0; i < timePoints.size(); i++) {
            timePointstoNum[i] = getMinutes(timePoints.get(i));
        }
        Arrays.sort(timePointstoNum);
        int preMinTime = timePointstoNum[1] - timePointstoNum[0];
        for (int i = 2; i < timePointstoNum.length; i++) {
            int curTime = timePointstoNum[i] - timePointstoNum[i - 1];
            preMinTime = Math.min(preMinTime, curTime);
        }
        //最后判断一下结束时间->开始时间，形成一个圆环
        preMinTime = Math.min(preMinTime, 24 * 60 + timePointstoNum[0] - timePointstoNum[timePointstoNum.length - 1]);
        return preMinTime;
    }

    private static int getMinutes(String timeStr) {
        return ((timeStr.charAt(0) - '0') * 10 + (timeStr.charAt(1) - '0')) * 60 + (timeStr.charAt(3) - '0') * 10 + (timeStr.charAt(4) - '0');
    }

}
